<?php
// 本类由系统自动生成，仅供测试用途
class GoodsAction extends BaseAction {
    
    public function index(){
    	$map['status'] = 1;

    	import('ORG.Util.Page');// 导入分页类
    	$count      = M('Goods')->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
    	$Page       = new Page($count);// 实例化分页类 传入总记录数
    	$show       = $Page->show();// 分页显示输出

		$list = M('Goods')->where($map)->limit($Page->firstRow,$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }

    public function set()
    {
    	if(IS_POST){
    		$id = $this->_post('id',0);
    		$data = array();
    		if($id){
                if($this->update()){
                    $this->_success('修改成功');
                }else{
                    $this->_error('修改失败');
                }
    		}else{

    			
    			if($this->insert()){
    				$this->_success('添加成功');
    			}else{
    				$this->_error('添加失败');
    			}
    		}
    	}else{
    		$this->display();
    	}
    }
    /**
     * 删除
     * @return [type] [description]
     */
    public function del(){
        $id = $this->_get('id',0);
        if($id){
            $re = M('Goods')->where(array('id'=>$id))->delete();
            if($re){
                $this->_success('删除成功');
            }else{
                $this->_error('删除失败');
            }
        }else{
            $this->_error('删除失败');
        }
    }
    public function insert(){
        $img = $this->upload();
        if( empty($img[0]) ){
            $this->_error('请上传商品图片');exit;
        }
        $data['title'] = $this->_post('title');
        $data['content'] = $this->_post('content','htmlspecialchars');
        $data['price'] = $this->_post('price');
        $data['saleprice'] = $this->_post('saleprice');
        $data['img'] = $img[0];
        $data['status'] = 1;
        $data['unit'] = $this->_post('unit');
        $data['create_time'] = $data['update_time'] = time();

        return $re = M('Goods')->add($data);
    }
    /**
     * 更新
     * @return [type] [description]
     */
    public function update(){
        $data =array();
        if($_FILES['img']['tmp_name']){
            $img = $this->upload();
            $data['img'] = $img[0];
        }
        $id = $this->_post('id',0);
        if($id){
            $data['title'] = $this->_post('title');
            $data['content'] = $this->_post('content','htmlspecialchars');
            $data['price'] = $this->_post('price');
            $data['saleprice'] = $this->_post('saleprice');
            
            $data['unit'] = $this->_post('unit');
            $data['update_time'] = time();

            $re = M('Goods')->where(array('id'=>$id))->save($data);
        }else{
            $re = false;
        }
        return $re;
        
    }
    public function edit()
    {
        $id = $this->_get('id',0);
        $row = M('Goods')->where(array('id'=>$id))->find();
        if($row){
            $this->assign('data',$row);
            $this->display('set');
        }else{
            $this->_error('不存在此商品');
        }
    }
    public function test(){

    }
}